﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Runda.CMS.Common.Utility
{
    public static class StringExtension
    {
        public static string FastReplace(this string original, string pattern, string replacement)
        {
            return FastReplace(original, pattern, replacement, StringComparison.InvariantCultureIgnoreCase);
        }

        public static string FastReplace(this string original, string pattern, string replacement, StringComparison comparisonType)
        {
            if (original == null)
                return null;

            if (String.IsNullOrEmpty(pattern))
                return original;

            int lenPattern = pattern.Length;
            int idxPattern = -1;
            int idxLast = 0;

            var result = new StringBuilder();

            while (true)
            {
                idxPattern = original.IndexOf(pattern, idxPattern + 1, comparisonType);

                if (idxPattern < 0)
                {
                    result.Append(original, idxLast, original.Length - idxLast);
                    break;
                }

                result.Append(original, idxLast, idxPattern - idxLast);
                result.Append(replacement);

                idxLast = idxPattern + lenPattern;
            }

            return result.ToString();
        }

        public static string ToSecuritySqlString(this string Original)
        {
            return Original.FastReplace("'", "");
        }

        /// <summary>
        /// 构建固定长度字符串，基于中英文字符判断
        /// </summary>
        /// <param name="inputString">原始字符串</param>
        /// <param name="mixLength">固定长度</param>
        /// <returns>构建后的字符串</returns>
        public static string FixLengthString(this string inputString, int mixLength)
        {
            var mixStringBuilder = new StringBuilder();
            int index = 0;
            int charLength = 0;
            int mixStringLengthCount = 0;

            for (index = 0; index < inputString.Length; index++)
            {
                char c = inputString.Substring(index, 1).ToCharArray()[0];
                if (c.IsUnicode())
                {
                    mixStringLengthCount += 2;
                    charLength = 1;
                }
                else
                {
                    mixStringLengthCount++;
                    charLength = 2;
                }

                if (mixStringLengthCount > mixLength)
                {
                    break;
                }
                else
                {
                    mixStringBuilder.Append(c.ToString());
                }
            }

            if (index < inputString.Length - 1)
            {
                mixStringBuilder.Remove(mixStringBuilder.Length - charLength - 1, charLength + 1);
                mixStringBuilder.Append("...");
            }

            return mixStringBuilder.ToString();
        }


        /// <summary>
        /// 检查输入字符是否为2字节字符
        /// </summary>
        /// <param name="inputChar">输入字符</param>
        /// <returns></returns>
        public static bool IsUnicode(this char inputChar)
        {
            return !(Convert.ToInt32(inputChar) < Convert.ToInt32(Convert.ToChar(128)));
        }

    }
}
